package stack.Exercise;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class poland {
    public static void main(String[] args) {
//        String str ="4 5 * 8 - 60 + 8 2 / +";
//        List<String> list = getList(str);
//        System.out.println(list.toString());
//        int calculate = calculate(list);
//        System.out.println(calculate);
        String str = "2.9-((2+3)*4)-5";
        List<String> list1 = getList1(str);
        System.out.println(list1);
        List<String> strings = paseToPoland(list1);
        System.out.println(strings);
        double calculate = calculate2(strings);
        System.out.println(calculate);
    }
    public static List<String> getList1(String s){
        //整数版本
//        String str="";
//        int index=0;
//        List<String>list = new ArrayList<>();
//        char c;
//        do{
//            if(((c=s.charAt(index))<48||(c=s.charAt(index))>57)){
//                    list.add(""+c);
//                    index++;
//            }else {
//                str="";
//                while ((index<s.length()&&(c=s.charAt(index))>=48&&(c=s.charAt(index))<=57)){
//                    str+=c;
//                    index++;
//                }
//                list.add(str);
//            }
//
//        }while (index<s.length());
        //小数点版本
        String str="";
        int index=0;
        List<String>list = new ArrayList<>();
        char c;
        do{
            if(((c=s.charAt(index))!=46)&&((c=s.charAt(index))<48||(c=s.charAt(index))>57)){
                list.add(""+c);
                index++;
            }else {
                str="";
                while ((index<s.length()&&((c=s.charAt(index))>=48&&(c=s.charAt(index))<=57)||
                        index<s.length()&&(c=s.charAt(index))==46)){
                    str+=c;
                    index++;
                }
                list.add(str);
            }

        }while (index<s.length());
//        String[] s1 = s.split("");
//        List<String> list = new ArrayList<>();
//        for (String s2 : s1) {
//            list.add(s2);
//        }
        return list;
    }
    public static List<String> getList(String str){
        String[] s = str.split(" ");
        List<String> list = new ArrayList<>();
        for (String s1 : s) {
            list.add(s1);
        }
        return list;
    }
    //整数版本
    public static int calculate(List<String>list){
        Stack<String> stack = new Stack();
        for (String s : list) {
           if(s.matches("\\d+")){//正则表达式
               stack.push(s);
           }else {
               int num1 = Integer.parseInt(stack.pop());
               int num2 = Integer.parseInt(stack.pop());
               int res = 0;
               if(s.equals("+")){
                   res=num1+num2;
               }else if(s.equals("-")){
                   res=num2-num1;
               }else if(s.equals("*")){
                   res=num1*num2;
               }else if(s.equals("/")){
                   res=num2/num1;
               }else throw new RuntimeException("输入法错误");
               stack.push(""+res);
           }
        }
        return Integer.parseInt(stack.pop());
    }
    //小数点版本
    public static double calculate2(List<String>list){
        Stack<String> stack = new Stack();
        for (String s : list) {
            if(s.matches("\\d+")||s.contains(".")){//正则表达式
                stack.push(s);
            }else {
                double num1 = Double.parseDouble(stack.pop());
                double num2 = Double.parseDouble(stack.pop());
                Double res = 0.0;
                if(s.equals("+")){
                    res=num1+num2;
                }else if(s.equals("-")){
                    res=num2-num1;
                }else if(s.equals("*")){
                    res=num1*num2;
                }else if(s.equals("/")){
                    res=num2/num1;
                }else throw new RuntimeException("输入法错误");
                stack.push(""+res);
            }
        }
        return Double.parseDouble(stack.pop());
    }
    public static int youxianji(String size){
        if ((size.equals("-")) || (size.equals("+"))){
            return 0;
        }else if (size.equals("*") || size.equals("/")){
            return 1;
        }
        return -1;
    }
        public static List<String> paseToPoland(List<String>list){
        Stack<String>s1 = new Stack<>();
        List<String> s2 = new ArrayList<>();
//            System.out.println("请输出一串需要运算的数字:");
//        String str = new Scanner(System.in).next();
            for (String s : list) {
                if(s.matches("\\d+")||s.contains(".")){
                    s2.add(s);
                }else if(s.equals("(")){
                    s1.push(s);
                }else if(s.equals(")")){
                    //去掉一对括号
                    while (!s1.peek().equals("(")){
                        s2.add(s1.pop());
                    }
                    s1.pop();
                }else{
                    while (s1.size()!=0 && youxianji(s1.peek())>=youxianji(s)){
                        s2.add(s1.pop());
                    }
                    s1.push(s);
                }
            }
            while (s1.size()!=0){
                s2.add(s1.pop());
            }
            return s2;
        }
}
